
#include "../../Utility/httplib.h"
#include "GNSSObsInput.hpp"

using namespace ns_GNSSObsInput;
using namespace httplib;
using namespace std;

void Usage(std::string proc)
{
    std::cerr << "Usage: " << "\n\t" << proc << " port" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }

    Server svr;

    svr.Get("/hello", [](const Request &req, Response &resp)
            { resp.set_content("nice to meet you too!", "text/plain;charset=utf-8"); });

    svr.Post("/ReadFile", [](const Request &req, Response &resp)
            {
        std::string inJson = req.body;
        if(GNSSObsInput::Start(inJson))
            resp.set_content("Finish!","text/plain;charset=utf-8"); 
        else 
            resp.set_content("Failed!","text/plain;charset=utf-8"); });

    svr.Post("/GetEpochRecord", [](const Request &req, Response &resp)
            {
        std::string inJson = req.body;
        std::string outJson;
        if(GNSSObsInput::GetEpochRecord(inJson,&outJson))
            resp.set_content(outJson,"application/json;charset=utf-8"); 
        else 
            resp.set_content("Failed!","text/plain;charset=utf-8"); });

    svr.Post("/RemoveFile", [](const Request &req, Response &resp)
             {
        std::string inJson = req.body;
        if(GNSSObsInput::RemoveTempFiles(inJson))
            resp.set_content("Finish!","text/plain;charset=utf-8"); 
        else 
            resp.set_content("Failed!","text/plain;charset=utf-8"); });

    svr.listen("0.0.0.0", atoi(argv[1]));

    return 0;
}